package com.tuosha.controller;

import com.tuosha.base.controller.BaseController;
import com.tuosha.constant.Constants;
import com.tuosha.model.JieQianBannersEntity;
import com.tuosha.service.JieQianBannersService;
import com.tuosha.util.Query;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author tuosha
 * @email 1044053532@qq.com
 * @date 2018-07-25 09:38:57
 */
@Controller
@RequestMapping("jieqianbanners")
public class JieQianBannersController extends BaseController {
	@Autowired
	private JieQianBannersService jieQianBannersServiceImpl;
	
	/**
	 * 页面
	 */
	@RequestMapping("/page")
	public String page(@RequestParam Map<String, Object> params){
		return "page/kouzijihe/jieqianbanners";
	}
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(@RequestParam Map<String, Object> params){

	    Query query = new Query(params);
		List<JieQianBannersEntity> jieQianBannersList = jieQianBannersServiceImpl.queryList(query);
		int total = jieQianBannersServiceImpl.queryTotal(query);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",total,jieQianBannersList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping(value="/info/{id}", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object info(@PathVariable("id") Integer id){
		JieQianBannersEntity jieQianBanners = jieQianBannersServiceImpl.queryObject(id);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,jieQianBanners);
	}

	/**
	 * 增加页面
	 */
	@RequestMapping("addBAR")
	public String barAdd(){
		return "page/kouzijihe/addKZBAR";
	}
	/**
	 * 保存
	 */
	@RequestMapping(value="/save", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@ModelAttribute JieQianBannersEntity jieQianBanners){
		jieQianBannersServiceImpl.save(jieQianBanners);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,jieQianBanners);
	}

	/**
	 * 修改待信息显示
	 */

	@RequestMapping(value="/editJieqianBanners/{id}")
	@RequiresPermissions("jieqianbanners:jieqianbanners:update")
	public String editJieqianBanners(HttpServletRequest req, @PathVariable("id")Integer id) {
		JieQianBannersEntity jieQianBannersEntities = jieQianBannersServiceImpl.queryObject(id);

		req.setAttribute("jieqianBanner",jieQianBannersEntities);

		return "page/kouzijihe/editJieqianBanners";
	}
	/**
	 * 修改
	 */
	@RequestMapping(value="/update", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@ModelAttribute JieQianBannersEntity jieQianBanners){
		System.out.println("xiugai~~~~~~~~~~");
		int result = jieQianBannersServiceImpl.update(jieQianBanners);
		return putMsgToJsonString(result,"",0,"");
	}

	@ResponseBody
	@RequestMapping(value = "/uploadFilee", method = RequestMethod.POST)
	public Object fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
		System.out.println("上传文件接口");
		String realPath = request.getSession().getServletContext().getRealPath("/")+"images\\";
		String basePath = request.getScheme()+"://"+request.getServerName()+":"
				+request.getServerPort() + request.getContextPath()+"/images/";
//		String realPath = request.getRequestURL()+"/images";
		System.out.println(basePath);
		String name = file.getOriginalFilename();
		if(!file.isEmpty()){
			FileUtils.copyInputStreamToFile(file.getInputStream(),new File(realPath,name));
		}
		System.out.println(basePath+name);
		String a = basePath+name;
		Object o = a;
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"提交成功！",0,o);
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(@RequestParam Integer[] ids){
		int result = jieQianBannersServiceImpl.deleteBatch(ids);
		return putMsgToJsonString(result,"",0,"");
	}
	
}
